﻿/**********************************************
 * 功能名称：中间体作业费率定义
编写人：  杨莹
建立时间： 杨 2012年7月6日
说明：
根据操作员的责任中心范围，列表显示当前作业中心的中间体。
选择成本细项类型，显示作业中心列表。对列表中的系数栏进行操作。
 *
 * 
 * 
 **************************************************************/
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Hisun.Models;
using Hisun.Controllers;
using System.Collections.Generic;

namespace Hisun.View.ProductInProcess
{
    public partial class PIP_IndexSetup : AbcPageBase
    {
        HisunDBDataContext db = new HisunDBDataContext();
        public string alert;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                var CurrentUserCenterId = (from a in db.TAbc_JS_Users where a.操作员ID == Session[ServerVars.sysSessionUserName].ToString() select new { a.责任部门ID }).Single();
                dropCostCenterSelect.Items.Clear();
                FillData2DropDownList(Convert.ToInt32(CurrentUserCenterId.责任部门ID));

                HiddenField_CurrentCenterId.Value = "";
                FillCostItemsList(); //

                Panel_Input.Visible = false;

            }
        }

        //填充成本细项列表框
        private void FillCostItemsList()
        {

            DropDownList_CostItemsList.Items.Clear();
            var MyCostItemsList = from a in db.TABC_DM_CostItems select new { a.成本细项ID, a.成本细项名称 };
            foreach (var MyCostItem in MyCostItemsList)
            {
                ListItem InsertItem = new ListItem(MyCostItem.成本细项ID + "|" + MyCostItem.成本细项名称, MyCostItem.成本细项ID);
                DropDownList_CostItemsList.Items.Add(InsertItem);
            }

        }

        //查询当前操作员对应的作业中心列表
        private void FillData2DropDownList(int CenterID)
        {
            ChildCenterList myCenter = new ChildCenterList(CenterID);

            var myCenterFilters = myCenter.CostCenterlist.Where(p => p.是否直接成本 == true && p.停用 == false);


            foreach (var MylistItem in myCenterFilters)
            {

                if (MylistItem.作业中心名称.Contains(TextBox_FilterText.Text))
                {
                    ListItem InsertItem = new ListItem(MylistItem.作业中心名称, MylistItem.作业中心ID.ToString());
                    dropCostCenterSelect.Items.Add(InsertItem);
                }
            }


        }

        //双击按钮的时候，填充当前的作业中心列表。
        protected void Button_fullCenterList_Click(object sender, EventArgs e)
        {
            var CurrentUserCenterId = (from a in db.TAbc_JS_Users where a.操作员ID == Session[ServerVars.sysSessionUserName].ToString() select new { a.责任部门ID }).Single();
            dropCostCenterSelect.Items.Clear();
            ListItem InsertItem = new ListItem("请选择", "");
            dropCostCenterSelect.Items.Add(InsertItem);
            FillData2DropDownList(Convert.ToInt32(CurrentUserCenterId.责任部门ID));

            HiddenField_CurrentCenterId.Value = "";
        }

        protected void Button_Query_Click(object sender, EventArgs e)
        {
            string CostCenterId = dropCostCenterSelect.SelectedValue;
            string CostItem = DropDownList_CostItemsList.SelectedValue;

            HiddenField_CurrentCenterId.Value = CostCenterId;
            HiddenField_CurrentCostItem.Value = CostItem;

            BindMyData(CostCenterId, CostItem);

        }


        private void BindMyData(string CostCenterId,string CostItem)
        {

            int MyCenterId = Convert.ToInt32(CostCenterId);

            var MyPIPQuery = from a in db.TABC_JS_ProductInProcess
                             join b in db.TABC_DM_ProductInProcess_PIPIndex on a.中间体ID equals b.中间体ID into temps
                             from temp in temps.Where(p=>p.中间体ID == a.中间体ID && p.成本细项ID == CostItem).DefaultIfEmpty()
                             where a.作业中心ID == MyCenterId
                             select new
                             {
                                 ID = a.ID,
                                 中间体ID = a.中间体ID,
                                 中间体名称 = a.中间体名称,
                                 计量单位 = a.计量单位,
                                 分配系数 = (temp == null ? 1:temp.中间体系数)
                             };

            

            if(MyPIPQuery.Count() != 0)
            {
                GridView_PIPList.DataSource = MyPIPQuery;
                GridView_PIPList.DataBind();
                Panel_Input.Visible = true;

            
            }else{

                Panel_Input.Visible = false;

                alert = "<script>alert('当前查询结果为空');</script>";
            
            }
            

        }

        protected void Button_Save_Click(object sender, EventArgs e)
        {



              int MyCenterId = Convert.ToInt32(HiddenField_CurrentCenterId.Value);
              string MyCostItem = HiddenField_CurrentCostItem.Value.ToString();




              if (GridView_PIPList.Rows.Count != 0)
              {



                  try
                  {
                      for (int i = 0; i < GridView_PIPList.Rows.Count; i++)
                      {
                          GridViewRow myrows = GridView_PIPList.Rows[i];

                          string MyPIPId = myrows.Cells[0].Text;
                          var MyEditRow = from a in db.TABC_DM_ProductInProcess_PIPIndex
                                          where a.作业中心ID == MyCenterId
                                          && a.成本细项ID == MyCostItem
                                          && a.中间体ID == MyPIPId
                                          select a;

                          //如果有多记录，先删除，再增加，保证数据唯一性。
                          if (MyEditRow.Count() != 0)
                          {
                              db.TABC_DM_ProductInProcess_PIPIndex.DeleteAllOnSubmit(MyEditRow);
                              db.SubmitChanges();

                          }

                          if (MyEditRow.Count() == 0)
                          {
                             

                              TABC_DM_ProductInProcess_PIPIndex MyAddRow = new TABC_DM_ProductInProcess_PIPIndex();

                              MyAddRow.公司ID = "hisun";
                              MyAddRow.账套ID = "001";
                              MyAddRow.中间体ID = myrows.Cells[0].Text;
                              MyAddRow.作业中心ID = MyCenterId;
                              MyAddRow.成本细项ID = MyCostItem;
                              MyAddRow.中间体系数 = (float)Convert.ToDecimal(((TextBox)myrows.Cells[3].FindControl("TextBox_Index")).Text);

                              db.TABC_DM_ProductInProcess_PIPIndex.InsertOnSubmit(MyAddRow);
                              db.SubmitChanges();
                          }




                      }
                

                      alert = "<script>alert('更新数据成功');</script>";

                      BindMyData(MyCenterId.ToString(), MyCostItem);
                  }
                  catch
                  {
                      alert = "<script>alert('更新数据失败');</script>";
                  }





              }

        }





    }
}
